/*
 * Copyright (c) 2006 - 2022, webrx.cn All rights reserved.
 *
 */
package cn.webrx;

/**
 * <p>Project: app2022 - Ticket
 * <p>Powered by webrx On 2022-01-27 11:46:45
 *
 * @author webrx [webrx@126.com]
 * @version 1.0
 * @since 17
 */
public class Ticket implements Runnable {
    private int num = 100;

    public Ticket() {
    }

    public Ticket(int num) {
        this.num = num;
    }

    @Override
    public void run() {
        String tn = Thread.currentThread().getName();
        System.out.printf("%s:线程启动%n", tn);
        while (true) {
            synchronized (this) {
                if (num < 1) {
                    break;
                }
                System.out.printf("%s:售出一张票，剩余%d张票%n", tn, --num);
                try {
                    Thread.sleep(100);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
        System.out.printf("%s:线程结束%n", tn);
    }
}

